/**
 * 和ajax结合的进度条
 */
function ProgressBar(pbId, canCancel, interval, value) {
	//有表示已经在运行
	if (document.getElementById(pbId)) return;
	
	this.pbId = pbId;
	this.canCancel = (canCancel)?canCancel:false;
	this.interval = (interval)?interval:500;
	this.value = (value)?value:0;
	this.canceled = false;
	
	//构造界面
	var sHtml = "<div id=\"" + this.pbId + "_window\" class=\"progress_window\">";
	sHtml += 		"<div id=\"" + this.pbId + "\" class=\"progress_div\">";
	sHtml += 			"<div class=\"progress_frame\" style=\"filter: Alpha(Opacity=0, FinishOpacity=100, Style=1, StartX=0, StartY=0, FinishX=100, FinishY=0);\">";
	sHtml += 				"<div class=\"progress_bar\"></div>";
	sHtml += 			"</div>";
	sHtml += 			"<div class=\"progress_value\">0%</div>";
	sHtml += 		"</div>";
	if (this.canCancel)
		sHtml += 		"<button id=\"" + this.pbId + "_button\" class=\"progress_button\">取消</button>";
	sHtml += 	"</div>";
	document.body.insertAdjacentHTML('beforeEnd', sHtml);
	
	//获取对象
	this.objWindow = document.getElementById(this.pbId + "_window");
	this.obj = document.getElementById(this.pbId);
	this.objFilter = this.obj.children[0];
	this.objValue  = this.obj.children[1];
	
	
	//可以取消
	if (this.canCancel) {
		this.objButton = document.getElementById(this.pbId + "_button");
		var oThis = this;
		this.objButton.onclick = function () {
			oThis.canceled = true;
			oThis.objButton.disabled = true;
		}
	} else
		this.objButton = null;
	
	//刷新进度
	this.refresh = function () {
		if (document.readyState == "complete") {
			if (this.objFilter.style.pixelWidth > 0) {
				var filterBackup = this.objFilter.style.filter;
				this.objFilter.style.filter = "";
				this.objFilter.style.filter = filterBackup;
			}
			
			this.objFilter.style.width = this.value + "%";
			this.objValue.innerText = this.value + "%";
		}
	}
	
	//运行过程
	this.run = function () {
		//判断进度
		//取消
		if (this.canCancel && this.canceled) {
			this.refresh();
			this.cancel();
		//结束
		} else if (this.value >= 100) {
			this.refresh();
			this.finish();
		//100毫秒后继续
		} else {
			this.refresh()
			var oThis = this;
			window.setTimeout(function () {oThis.run()}, this.interval);
		}
		
		//获取进度
		if (typeof this.progressURL == "string") {
			var http = new HttpObject();
			var p = http.httpGetReturnString(this.progressURL);
			if (isInvalid(p) && !isNaN(p))
				this.value = new Number(p);
			else
				this.value += 5
		} else if (typeof this.onProgress == "function") {
			var p = this.onProgress();
			if (isInvalid(p) && !isNaN(p)) 
				this.value = new Number(p);
			else
				this.value += 5
		} else
			this.value += 5;
	}
	
	//开始
	this.start = function (w_width, w_height,w_left, w_top) {
		//show
		if (!w_width) w_width = 300;
		if (!w_height) w_height = 20;
		if (!w_top) w_top = (screen.height - w_height) / 2;
		if (!w_left) w_left = (screen.width - w_width) / 2;
		this.objWindow.style.width = w_width;
		this.objWindow.style.height = w_height;
		this.objWindow.style.left = w_left;
		this.objWindow.style.top = 200;
		this.obj.style.width = w_width - 100;
		this.objWindow.style.display = "block";
		
		//触发事件
		if (typeof this.startURL == "string") {
			var http = new HttpObject();
			var ret = http.httpGetReturnString(this.startURL);
			if (ret == 'false') return;
			
		} else if (typeof this.onStart == "function") {
			var success = this.onStart();
			if (!success) return;
		}
		
		//run
		this.run();
	}
	
	//结束
	this.finish = function () {
		//触发结束的事件
		if (typeof this.finishURL == "string") {
			var http = new HttpObject();
			var ret = http.httpGetReturnString(this.finishURL);	
			if (ret == 'false') return;
		} else if (typeof this.onFinish == "function") {
			var success = this.onFinish();
			if (!success) return;
		}
		//unshow
		this.objWindow.style.display = "none";
		//remove
		document.body.removeChild(this.objWindow);
	}
	
	//取消
	this.cancel = function () {
		//触发结束的事件
		if (typeof this.cancelURL == "string") {
			var http = new HttpObject();
			var ret = http.httpGetReturnString(this.cancelURL);	
			if (ret == 'false') return;		
		} else if (typeof this.onCancel == "function") {
			var success = this.onCancel();
			if (!success) return;
		}
		//unshow
		this.objWindow.style.display = "none";
		//remove
		document.body.removeChild(this.objWindow);
	}
	
	//开始的URL
	this.startURL = null;
	//获取进度的URL
	this.progressURL = null;
	//取消的URL
	this.cancelURL = null;
	//完成的URL
	this.finishURL = null;
	
	
	//开始进度时触发的事件
	this.onStart = null;
	//获取progress的事件
	this.onProgress = null;
	//取消事件
	this.onCancel = null;
	//完成的事件
	this.onFinish = null;
}